package com.example.campuslink.viewModel

import android.content.Intent
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.campuslink.commen.db.message.MessageTable
import com.example.campuslink.commen.db.userInfo.UserInfoTable
import com.example.campuslink.commen.enums.MessageTypeEnum
import com.example.campuslink.commen.tools.Sp
import com.example.campuslink.commen.tools.SpKey
import com.example.campuslink.commen.tools.putExtraKey
import com.example.campuslink.ui.activity.UserMainActivity
import com.example.campuslink.ui.activity.userOperation.UserInfoActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class FansAndAttentionViewModel : BaseViewModel() {
    private val _sortTime = MutableLiveData<String>()
    val sortTime get() = _sortTime
    private val _userList = MutableLiveData<MutableList<String>>()
    val userList: LiveData<MutableList<String>> = _userList
    private val _type = MutableLiveData<String>()
    val type: LiveData<String> = _type
    private val _attentionList = MutableLiveData<MutableList<String>>()
    val attentionList: LiveData<MutableList<String>> = _attentionList
    private val _fansList = MutableLiveData<MutableList<String>>()
    val fansList: LiveData<MutableList<String>> = _fansList
    private val _userInfoMap = MutableLiveData<HashMap<String, UserInfoTable>>()
    val userInfoMap: LiveData<HashMap<String, UserInfoTable>> = _userInfoMap
    var jumpNumberString: String? = ""
    fun setType(type: String) {
        _type.value = type
    }

    fun setSortTime(time: String) {
        _sortTime.value = time
    }

    fun findUserInfo(userId: String,nowNumber: String){
        val temp = mutableListOf<String>()
        viewModelScope.launch(Dispatchers.IO) {
            val number =nowNumber
            val userNumberTableInfo = userNumberDao.getUserNumber(number ?: "")
            if(_type.value==putExtraKey.ATTENTION.key){
                userNumberTableInfo?.followList?.forEach {
                    if(it.contains(userId)){
                        temp.add(it)
                    }
                }
            }else{
                userNumberTableInfo?.fansList?.forEach {
                    if(it.contains(userId)){
                        temp.add(it)
                    }
                }
            }
            _userList.postValue(temp)
        }
    }

    fun removeAttentionAndFans(userId: String, nowNumber: String) {
        viewModelScope.launch(Dispatchers.IO) {
            val userNumber = nowNumber
            val nowUserNumberTableInfo = userNumberDao.getUserNumber(userNumber ?: "")
            val otherUserNumberTableInfo = userNumberDao.getUserNumber(userId ?: "")
            nowUserNumberTableInfo?.followList?.removeAll { it == userId }
            otherUserNumberTableInfo?.fansList?.removeAll { it == userNumber }
            userNumberDao.updateUserNumber(nowUserNumberTableInfo ?: return@launch)
            userNumberDao.updateUserNumber(otherUserNumberTableInfo ?: return@launch)
            if (_type.value == putExtraKey.FANS.key) {
                getFansList(nowNumber)
            } else {
                getAttentionList(nowNumber)
            }
            getAttentionListAndFansList(nowNumber)
        }
    }

    fun addAttentionAndFans(userId: String, nowNumber: String) {
        viewModelScope.launch(Dispatchers.IO) {
            val userNumber = nowNumber
            val nowUserNumberTableInfo = userNumberDao.getUserNumber(userNumber ?: "")
            val otherUserNumberTableInfo = userNumberDao.getUserNumber(userId ?: "")
            nowUserNumberTableInfo?.followList?.removeAll { it == userId }
            otherUserNumberTableInfo?.fansList?.removeAll { it == userNumber }
            nowUserNumberTableInfo?.followList?.add(userId)
            otherUserNumberTableInfo?.fansList?.add(userNumber ?: "")
            val schoolInfoTable =
                schoolInfoDao.getSchoolInfoBySchoolName(nowUserNumberTableInfo?.schoolName ?: "")
            if (schoolInfoTable != null) {
                var messageTable = MessageTable()
                messageTable.handNumber = userId
                messageTable.sendNumber = userNumber ?: ""
                messageTable.messageType = MessageTypeEnum.NEW_ADD_ATTENTION.value
                messageTable.messageTime = System.currentTimeMillis()
                messageTable.messageContent = "对方关注了你"
                messageTable.messageImage = ""
                MessageTable.insertWithId(messageDao, messageTable)
                schoolInfoTable?.messageList?.add(messageTable)
                schoolInfoDao.updateSchoolInfo(schoolInfoTable)
            }
            userNumberDao.updateUserNumber(nowUserNumberTableInfo ?: return@launch)
            userNumberDao.updateUserNumber(otherUserNumberTableInfo ?: return@launch)
            if (_type.value == putExtraKey.FANS.key) {
                getFansList(nowNumber)
            } else {
                getAttentionList(nowNumber)
            }
            getAttentionListAndFansList(nowNumber)
        }
    }

    fun getAttentionListAndFansList(nowNumber: String) {
        viewModelScope.launch(Dispatchers.IO) {
            val userNumber = nowNumber
            val userNumberTableInfo = userNumberDao.getUserNumber(userNumber ?: "")
            _attentionList.postValue(userNumberTableInfo?.followList)
            _fansList.postValue(userNumberTableInfo?.fansList)
        }
    }

    fun getAttentionList(nowNumber: String) {
        viewModelScope.launch(Dispatchers.IO) {
            val number =nowNumber
            val userNumberTableInfo = userNumberDao.getUserNumber(number ?: "")
            _userList.postValue(userNumberTableInfo?.followList)
        }
    }

    fun getUserInfo(userId: String) {
        viewModelScope.launch(Dispatchers.IO) {
            try {
                val userInfo = userInfoDao.getUserInfoByUserNumber(userId) ?: return@launch
                val currentMap = _userInfoMap.value ?: HashMap()
                currentMap[userId] = userInfo
                _userInfoMap.postValue(currentMap)
            } catch (e: Exception) {
                Log.e("TAG", "获取用户信息失败", e)
            }
        }
    }

    fun getFansList(nowNumber: String) {
        viewModelScope.launch(Dispatchers.IO) {
            val number = nowNumber
            val userNumberTableInfo = userNumberDao.getUserNumber(number ?: "")
            _userList.postValue(userNumberTableInfo?.fansList)
        }
    }

    fun jumpToUserInfoActivity(userNumber: String) {
//        Sp.setString(context, SpKey.OTHER_NUMBER.key, userNumber)
        startActivity(Intent().apply {
            putExtra(putExtraKey.USER_NUMBER.key, userNumber)
        }, targetClass = UserMainActivity::class.java)
    }
}